package com.example.demo2.controller;


import com.example.demo2.bean.UserBean;
import com.example.demo2.bean.YourBean;
import com.example.demo2.service.UserBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*",methods ={RequestMethod.GET, RequestMethod.OPTIONS, RequestMethod.POST})
public class UserBeanController {

    @Autowired
    UserBeanService userBeanService;

    //登录
    @RequestMapping("/user/login")
    public Map<String, Object> getMember(@RequestBody UserBean userBean) {
        Map<String, Object> map = new HashMap<>();
        UserBean userBean1 = userBeanService.login(userBean);
        if (userBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    //注册
    @RequestMapping("/user/register")
    public Map<String, Object> addMember(@RequestBody UserBean userBean) {
        Map<String, Object> map = new HashMap<>();
        UserBean userBean1 = userBeanService.register(userBean);
        if (userBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    // 查询一个
    @RequestMapping("/user/findone")
    public Map<String, Object> findone(@RequestBody UserBean userBean) {
        Map<String, Object> map = new HashMap<>();
        UserBean userBean1 = userBeanService.findone(userBean);
        if (userBean1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", userBean1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    // 查询所有
//    @RequestMapping("/YourServer/yourbean/list")
//    public Map<String, Object> list() {
//        Map<String, Object> map = new HashMap<>();
//        List<YourBean> list = yourBeanService.list();
//        if (list != null) {
//            map.put("success", true);
//            map.put("msg", "成功");
//            map.put("list", list);
//        } else {
//            map.put("success", false);
//            map.put("msg", "失败");
//        }
//        return map;
//    }

    // 删除
    @RequestMapping("/user/delete")
    public Map<String, Object> delete(@RequestBody UserBean userBean) {
        Map<String, Object> map = new HashMap<>();
        try {
            userBeanService.deleteById(userBean.getUser_id());
            map.put("success", true);
            map.put("msg", "成功");
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", "失败");
        }

        return map;
    }
    // 修改
    @RequestMapping("/user/update")
    public Map<String, Object> update(@RequestBody UserBean userBean) {
        Map<String, Object> map = new HashMap<>();
        UserBean userBean1 = userBeanService.update(userBean);
        if (userBean != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", userBean1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
//    // 添加
//    @RequestMapping("/user/addone")
//    public Map<String, Object> addone(@RequestBody UserBean userBean) {
//        Map<String, Object> map = new HashMap<>();
//        UserBean userBean1 = userBeanService.addone(userBean);
//        if (userBean1 != null) {
//            map.put("success", true);
//            map.put("msg", "成功");
//            map.put("data", userBean1);
//        } else {
//            map.put("success", false);
//            map.put("msg", "失败");
//        }
//        return map;
//    }
}
